package cn.tedu.mall.search.test;

import java.util.LinkedList;
import java.util.Scanner;

public class ChouShu2 {
    public static void main(String[] args) {
        while (true) {
            System.out.println("求第几个丑数：");
            int n = new Scanner(System.in).nextInt();
            long result = f(n);
            System.out.printf("第%d个丑数是：%d\n", n, result);
        }
    }

    private static long f(int n) {
        LinkedList<Long> list2 = new LinkedList<>();
        LinkedList<Long> list3 = new LinkedList<>();
        LinkedList<Long> list5 = new LinkedList<>();
        list2.add(2L);
        list3.add(3L);
        list5.add(5L);
        for (int i = 1; ; i++) {
            Long a = list2.getFirst();
            Long b = list3.getFirst();
            Long c = list5.getFirst();
            Long min = Math.min(a,Math.min(b,c));
            if (i == n) return min;
            if (min.equals(a)) list2.removeFirst();
            if (min.equals(b)) list3.removeFirst();
            if (min.equals(c)) list5.removeFirst();
            list2.add(min*2);
            list3.add(min*3);
            list5.add(min*5);
        }
    }
}
